import {
  createSlice
} from '@reduxjs/toolkit'

// 异步 action {type: 'whenOddIncreaseAction/fulfilled', 函数返回值}
/* export const whenOddIncreaseAction = createAsyncThunk('whenOddIncreaseAction', (data, store) => {
  const {count} = store.getState().countReducer
  if (count % 2 !== 0) {
    return data
  }
  return 0
}) */

// 模拟 createSlice 生成的 action
/* const increase = (data) => {
  return {
    type: 'count_increase',
    data
  }
} */


const countSlice = createSlice({
  name: 'count',
  initialState: {
    count: 0
  },
  reducers: {
    increase(state, {
      payload
    }) {
      state.count += payload
    },
    decrease(state, {
      payload
    }) {
      state.count -= payload
    }
  },
  /* extraReducers: {  // 生成异步处理的 reducer
    [whenOddIncreaseAction.fulfilled](state, {payload}) {
      state.count += payload
    }
  } */
})

export default countSlice.reducer

export const {
  increase,
  decrease
} = countSlice.actions